<template>
	<view class="page">
		<view class="tabs">
			<view :class="[tabsIndex==0?'tabsActive':'tabsText']" @click="tabsIndex=0">下属</view>
			<view :class="[tabsIndex==1?'tabsActive':'tabsText']" @click="tabsIndex=1">关注</view>
		</view>
		
		<view class="staffList" v-show="tabsIndex==0">
			<view class="input">
				<icon type="search" size="18"/>
				<input v-model="userName" placeholder="搜索员工" />
			</view>
			<view v-for="(item,index) in employeeList" :key="item.userId" @click="staffClick(item)">
				<view class="staffList-item" >
					<view class="staffList-item-info">
						<view class="staffList-item-info-surname">{{item.userName.substr(0,1)}}</view>
						<view class="staffList-item-info-name">{{item.userName}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="staffList" v-show="tabsIndex==1">
			<view class="staffList-item" v-for="(item,index) in focusOnList" :key="item.userId">
				<view class="staffList-item-info">
					<view class="staffList-item-info-surname">{{item.userName.substr(0,1)}}</view>
					<view class="staffList-item-info-name">{{item.userName}}</view>
				</view>
				<view class="staffList-item-concern">
					<button v-if="item.isAttention" style="font-size: 26rpx;"  @click="attention(item,'true')">已关注</button>
					<button v-else type="primary" class="attention" style="" @click="attention(item,'false')">+ 关注</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	import * as api from '@/utils/api.js';
	export default {
		data() {
			return {
				tabsIndex:0,
				userName:'',
				employeeList:[],
				focusOnList:[],
				flg:false
			}
		},
		onLoad(option) {
			this.getEmployees()
		},
		watch: {
			// 搜索框防抖
			userName(newVal){
				if (this.flg) {
				  return false
				}
				this.flg = true
				setTimeout(()=> {
					this.getEmployees()
				  this.flg = false
				}, 800)
			}
		},
		methods: {
			getEmployees(){
				api.getEmployees({userName:this.userName}).then(res=>{
					if(res.code == 0){
						let { employeeList ,focusOnList } = res.data.result
						this.employeeList = employeeList
						focusOnList.forEach(item=>{
							item.isAttention = true
						})
						this.focusOnList = focusOnList
					}
				})
			},
			attention(item,type){ 
				if(type=='true'){ // 取消关注
					api.unsubscribe(item.userId).then(res=>{
						if(res.code == 0){
							item.isAttention = false
							uni.showToast({
								title:'取消关注成功',
								icon:'none',
								duration: 2000
							});	
						}
					})
				}else{ // 关注
					let data = {
						attentionUserId:item.userId,
						attentionUserName:item.userName
					}
					api.focusOn(data).then(res=>{
						if(res.code == 0){
							item.isAttention = true
							uni.showToast({
								title:'关注成功',
								icon:'none',
								duration: 2000
							});	
						}
					})
				}
			},
			staffClick(item){
				let pages = getCurrentPages()
				let prePage = pages[pages.length - 2]
				prePage.setData({
					newUserName:item.userName,
					newUserId:item.userId
				})
				uni.navigateBack();
			}
			
		}		
	}
</script>

<style lang="scss" scoped>
	.page {
		.tabs{
			width: 100%;
			display: flex;
			.tabsText{
				color: #818181;
				font-weight: 700;
				height: 80rpx;
				background-color: #fbfbfb;
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			.tabsActive{
				font-weight: 700;
				height: 80rpx;
				background-color: #fbfbfb;
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #4b4b4b;
				border-bottom: 6rpx solid #4b4b4b;
			}
		}
		.input{
			padding: 20rpx 30rpx;
			display: flex;
			align-items: center;
			background: #eee;
			border-radius: 60rpx;
			margin: 26rpx 30rpx;
			input{
				width: 100%;
				margin-left: 10rpx;
			}
		}
		.staffList{
			margin-top: 15rpx;
			&-item{
				padding: 0 45rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;
				&-info{
					display: flex;
					align-items: center;
					&-surname{
						margin-right: 20rpx;
						width: 90rpx;
						height: 90rpx;
						border-radius: 45rpx;
						background-color: #f6c255;
						display: flex;
						align-items: center;
						justify-content: center;
						color: #fff;
						font-size: 38rpx;						
					}
					&-name{
						font-weight: 700;
					}
				}
				&-concern{
					width: 160rpx;
					.attention{
						background-color: #409eff;
						color: #fff;
						font-size: 26rpx;
					}
				}
			}
		}
	}
</style>
